#! /usr/local/bin/python
"""
Author: Andy Nagels
Date: 2010-01-30
newmail.py: Create an alert when a new mail is received.

Copyright (C) 2010 Andy Nagels

This file is part of Alertlogger.

Alertlogger is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Alertlogger is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Alertlogger.  If not, see <http://www.gnu.org/licenses/>.
					
"""
class Alert:

    def get_alert(self):
        """ Get the alertstring for this alert. """
        return self._alert

    def set_alert(self, msg):
        """ Set an alert. """
        self._alert = msg

    alert = property(get_alert, set_alert)

    def get_logloc(self):
        """ Get the logfile location for this alert. """
        return self._logloc

    def set_logloc(self, value):
        """ Set the logfile location. """
        self._logloc = value

    logloc = property(get_logloc, set_logloc)

    def get_enabled(self):
        """ Get the enabledstate. """
        return self._enabled

    def set_enabled(self, state):
        """ Set the enabledstate. """
        self._enabled = state

    enabled = property(get_enabled, set_enabled)

    def get_description(self):
        """ Get the description. """
        return self._description

    def set_description(self, desc):
        """ Set the description. """
        self._description = desc 

    description = property(get_description, set_description

    def __init__(self):
        """ Initialize alert class. """
        self.set_alert("") # Do not change this line!

        # Enter a description for this alert-file here:
        self.set_description("Parses the output of maildirtree and checks for new mails in all available maildir accounts on the systems maildir folder.")
        # Enable (1) or disable (0) this alert-file:
        self.set_enabled(1)
        # Location of logfile
        self.set_logloc("log/newmail.log")

    def fCommand(self):
        """ Command to execute, this will create an alert-message. """
        # The variable alertmsg, will have to contain either an alertmsg 
        # or an empty string.
        # When it's an empty string, nothing will be logged.
        
        ### Only edit the values below.
        ## Variables
        
        ## Code to generate alert
        # Your custom alert code goes here:
        
        import re, sys, subprocess

        mail = "~/mail/acc/*"
        # Parse the output of maildirtree ~/mail/* | grep total
        # and if the first number > 0, then alertmsg = the entire contents of
        # maildirtree ~/mail/* (without the grep)
        #cmd1 = os.popen("maildirtree ~/mail/acc/* | grep total").read()
        #cmd2 = subprocess.Popen("maildirtree", 0, "/home/rockwolf/mail/acc/thereisanewway_gmail", stdout=subprocess.PIPE, shell=False).communicate()
        print mail
        cmd2 = subprocess.Popen('maildirtree ' + mail, shell=True).communicate()
        print "1:" + str(cmd2)
        #print "2:" + str(sys.stdout.read())
        srch = re.search('Unread',str(cmd2))
        print srch
        alertmsg = '...'
        ### Do not edit beyond this point.

        self.set_alert(alertmsg)
